from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains


# 登录页面
class LoginPage(object):
    def __init__(self, driver):
        # 浏览器驱动
        self.driver = driver

    # 页面地址
    def get_url(self, url):
        self.driver.get(url)

    # 用户登录按钮
    def user_button(self):
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/header/div[2]/button/span').click()
        except Exception as e:
            message = '下拉框元素定位失败'
            print(e)
            print(message)

    def user_login(self):
        try:
            self.user_button()
            # 用户名输入框
            self.usernameBy = self.driver.find_element(By.ID, 'user_login')

            # 密码输入框
            self.passwordBy = self.driver.find_element(By.ID, 'user_password')

            # 登录按钮
            self.submitBy = self.driver.find_element(By.XPATH, '//*[@id="new_user"]/div/div/div/div[4]/input')
        except Exception as e:
            message = '登录元素定位失败'
            print(e)
            print(message)

    # 登录功能
    def login_valid(self, username, password):
        # 输入用户名
        self.usernameBy.send_keys(username)
        # 输入密码
        self.passwordBy.send_keys(password)
        # 点击登录按钮
        self.submitBy.click()

    # 用户登录成功后点击用户下拉框
    def user_selector(self):
        try:
            self.driver.find_elements(By.XPATH, '//*[@id="app"]/div[1]/header/div[2]/div[1]')[0].click()
        except Exception as e:
            message = '下拉框元素定位失败'
            print(e)
            print(message)

    # 用户退出按钮
    def user_logout(self):
        try:
            ul = self.driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div/div[1]/div/ul')
            ul.find_elements(By.XPATH, 'li')[1].click()
        except Exception as e:
            message = '退出按钮元素定位失败'
            print(e)
            print(message)

    # 用户空间按钮
    def user_page(self):
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div/div[1]/div/ul/li[1]').click()
        except Exception as e:
            message = '用户空间按钮元素定位失败'
            print(e)
            print(message)

    # 镜像定制查看按钮
    def image_build(self):
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/section/main/div[2]/div['
                                               '1]/button/span').click()
        except Exception as e:
            message = '镜像定制查看按钮元素定位失败'
            print(e)
            print(message)

    # 构建工程查看按钮
    def projects_build(self):
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div/section/main/div[1]/div['
                                               '1]/button/span').click()
        except Exception as e:
            message = '构建工程查看按钮元素定位失败'
            print(e)
            print(message)

    # 语言栏选择界面
    def switch_language(self):
        try:
            mouse = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/header/div[2]/div/div/div')
            ActionChains(self.driver).move_to_element(mouse).perform()
        except Exception as e:
            message = '语言栏选择界面元素定位失败'
            print(e)
            print(message)

    # 切换英文
    def switch_English(self):
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[last()]/div/div/div[1]/div/ul/li[2]').click()
        except Exception as e:
            message = '英文按钮元素定位失败'
            print(e)
            print(message)

    # 切换中文
    def switch_Chinese(self):
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[last()]/div/div/div[1]/div/ul/li[1]').click()
        except Exception as e:
            message = '中文按钮元素定位失败'
            print(e)
            print(message)

    # 获取弹窗元素
    def alert_message(self):
        mouse_alert = self.driver.find_element(By.XPATH, '/html/body/div[3]')
        ActionChains(self.driver).move_to_element(mouse_alert).perform()
        alert = self.driver.find_element(By.XPATH, '/html/body/div[3]')

        return alert.text
